<template>
  <div class="search-div">
    <!-- 搜索表单 -->
    <el-form label-width="70px" size="small">
      <el-form-item label="医师姓名">
        <el-input v-model="doctor.name" style="width: 120px" placeholder="医师姓名"></el-input>
      </el-form-item>
      <el-form-item label="工号">
        <el-input v-model="doctor.workerNumber" style="width: 120px" placeholder="工号"></el-input>
      </el-form-item>
      <el-form-item label="科室">
        <el-select v-model="doctor.department" placeholder="请选择">
          <el-option v-for="item in division" :key="item.id" :label="item.sname" :value="item.id">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="职称">
        <el-select v-model="doctor.title" placeholder="请选择">
          <el-option v-for="item in protitle" :key="item.protid" :label="item.professionaltitle" :value="item.protid">
          </el-option>
        </el-select>
      </el-form-item>
      <el-row style="display:flex">
        <el-button type="primary" size="small" @click="searchSysRole">
          搜索
        </el-button>
        <el-button size="small" @click="resetData">重置</el-button>
      </el-row>
    </el-form>

    <!-- 添加按钮 -->
    <div class="tools-div">
      <el-button type="success" size="small" @click="addShow">添 加</el-button>
    </div>
    <!-- 添加角色表单对话框 -->
    <!-- 页面表单 -->
    <el-dialog v-model="dialogVisible" title="添加或修改角色" width="30%">
      <el-form label-width="120px">
        <el-form-item label="头像">
          <el-upload class="avatar-uploader" action="http://localhost:8501/admin/system/fileUpload"
            :show-file-list="false" :on-success="handleAvatarSuccess" :headers="headers">
            <img v-if="doctor.avatar" :src="doctor.avatar" class="avatar" />
            <el-icon v-else class="avatar-uploader-icon">
              <Plus />
            </el-icon>
          </el-upload>
        </el-form-item>
        <el-form-item label="医生姓名">
          <el-input v-model="doctor.name" />
        </el-form-item>
        <el-form-item label="年龄">
          <el-input v-model="doctor.age" />
        </el-form-item>
        <el-form-item label="性别">
          <el-radio-group v-model="doctor.gender">
            <el-radio :label="1">男</el-radio>
            <el-radio :label="2">女</el-radio>
          </el-radio-group>
        </el-form-item>

        <el-form-item label="科室">
          <el-select v-model="doctor.department" placeholder="请选择">
            <el-option v-for="item in division" :key="item.id" :label="item.sname" :value="item.id">
            </el-option>
          </el-select>
        </el-form-item>

        <el-form-item label="职称">
          <el-select v-model="doctor.title" placeholder="请选择">
            <el-option v-for="item in protitle" :key="item.protid" :label="item.professionaltitle" :value="item.protid">
            </el-option>
          </el-select>
        </el-form-item>

        <el-form-item label="工号">
          <el-input v-model="doctor.workerNumber" />
        </el-form-item>
        <el-form-item label="主治症状">
          <el-input v-model="doctor.mainSymptom" />
        </el-form-item>
        <el-form-item label="身份证号">
          <el-input v-model="doctor.identityCard" />
        </el-form-item>
        <el-form-item label="联系电话">
          <el-input v-model="doctor.phone" />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="submit">提交</el-button>
          <el-button @click="diso">取消</el-button>
        </el-form-item>
      </el-form>
    </el-dialog>

    <!--- 角色表格数据 -->
    <el-table :data="list" style="width: 100%">
      <el-table-column prop="avatar" label="头像" #default="scope">
        <img :src="scope.row.avatar" width="50" />
      </el-table-column>
      <el-table-column prop="name" label="医师姓名" width="100" />
      <el-table-column prop="age" label="年龄" width="100" />
      <el-table-column prop="gender" label="性别" width="100" #default="scope">
        {{ scope.row.gender == 1 ? '男' : '女' }}
      </el-table-column>
      <el-table-column prop="sname" label="科室" width="100" />
      <el-table-column prop="professionaltitle" label="职称" width="100" />
      <el-table-column prop="workerNumber" label="工号" width="100" />
      <el-table-column prop="mainSymptom" label="主治症状" width="100" />
      <el-table-column prop="identityCard" label="身份证号" width="180" />
      <el-table-column prop="phone" label="联系电话" width="120" />
      <el-table-column prop="createTime" label="创建时间" width="130" />
      <el-table-column prop="updateTime" label="修改时间" width="130" />
      <el-table-column label="操作" align="center" width="280" #default="scope">
        <el-button type="primary" size="small" @click="editShow(scope.row)">
          修改
        </el-button>
        <el-button type="danger" size="small" @click="deleteById(scope.row)">
          删除
        </el-button>
      </el-table-column>
    </el-table>

    <!-- <el-pagination v-model:current-page="pageParams.page" v-model:page-size="pageParams.limit"
      :page-sizes="[5, 10, 20, 50, 100]" @size-change="fetchData" @current-change="fetchData"
      layout="total, sizes, prev, pager, next" :total="total" /> -->
  </div>

</template>

<!-- script部分修改内容 -->
<script setup>
import { useApp } from '@/pinia/modules/app'
import { ref, onMounted } from 'vue';
import { findDoctorAll, addDoctor, updateDoctor, deleteDoctor } from "@/api/doctor/doctor";
import { findProfessionalAll, findDivision} from "@/api/doctor/prodtp";
import { ElMessage, ElMessageBox } from 'element-plus'
// 分页条总记录数
let total = ref(0)

// 定义表格数据模型
let list = ref([])
const defaultProps = {
  children: 'children',
  label: 'title',
}
//定义职称
let protitle = ref([])
//定义科室
let division = ref([])
const dialogMenuVisible = ref(false)
const sysMenuTreeList = ref([])
//分页数据
const pageParamsForm = {
  page: 1, // 页码
  limit: 5, // 每页记录数
}
const pageParams = ref(pageParamsForm)     // 将pageParamsForm包装成支持响应式的对象


//定义医师数据模型
const defaultForm = {
  id: "",
  name: "",
  age: "",
  gender: "",
  department: "",
  title: "",
  workerNumber: "",
  mainSymptom: "",
  identityCard: "",
  phone: "",
  avatar: ""
}
const doctor = ref(defaultForm)

// 页面加载完毕以后请求后端接口获取数据
onMounted(() => {
  fetchData()
  findProtitle()
  findDivisions()
})

// 搜索按钮点击事件处理函数
const searchSysRole = () => {
  fetchData();
}

// 重置按钮点击事件处理函数
const resetData = () => {
  doctor.value.name = ""
  doctor.value.workerNumber = ""
  doctor.value.title = ""
  doctor.value.department=""
  fetchData();
}


// 远程调用后端分页查询接口
const fetchData = async () => {
  const { data, code, message } = await findDoctorAll();
  console.log(data);
  list.value = data;
  //total.value = data.total
}

//添加
// 控制对话是否展示的变量
const dialogVisible = ref(false)
const diso = () => {
  dialogVisible.value = false
  doctor.value = {}
}
//进入添加
const addShow = () => {
  dialogVisible.value = true
  doctor.value = {}
  findProtitle()
  findDivisions()
}
//查询全部职称
const findProtitle = async () => {
  const { data, code } = await findProfessionalAll();
  protitle.value = data
}
//查询全部科室
const findDivisions = async () => {
  const { data, code } = await findDivision();
  division.value = data.list
}
//提交
const submit = async () => {
  if (!doctor.value.id) {
    const { code } = await addDoctor(doctor.value);
    if (code === 200) {
      dialogVisible.value = false
      ElMessage.success('添加成功')
      doctor.value = {}
      fetchData()
    }
  } else {
    const { code } = await updateDoctor(doctor.value);
    if (code === 200) {
      dialogVisible.value = false
      ElMessage.success('修改成功')
      doctor.value = {}
      resetData()
    }
  }
}
//删除
const deleteById = (row) => {
  ElMessageBox.confirm('此操作将永久删除该记录, 是否继续?', 'Warning', {
    confirmButtonText: '确定',
    cancelButtonText: '取消',
    type: 'warning',
  }).then(async () => {
    const { code } = await deleteDoctor(row.id)
    if (code === 200) {
      ElMessage.success('删除成功')
      pageParams.value.page = 1
      fetchData()
    }
  })
}
//修改回显
const editShow = (row) => {
  dialogVisible.value = true
  doctor.value = row
  findProtitle()
}

const headers = {
  token: useApp().authorization.token     // 从pinia中获取token，在进行文件上传的时候将token设置到请求头中
}

// 图像上传成功以后的事件处理函数
const handleAvatarSuccess = (response, uploadFile) => {
  doctor.value.avatar = response.data
}

</script>

<style scoped>
.search-div {
  margin-bottom: 10px;
  padding: 10px;
  border: 1px solid #ebeef5;
  border-radius: 3px;
  background-color: #fff;
}

.tools-div {
  margin: 10px 0;
  padding: 10px;
  border: 1px solid #ebeef5;
  border-radius: 3px;
  background-color: #fff;
}

.avatar-uploader .avatar {
  width: 178px;
  height: 178px;
  display: block;
}

.avatar-uploader .el-upload {
  border: 1px dashed var(--el-border-color);
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
  transition: var(--el-transition-duration-fast);
}

.avatar-uploader .el-upload:hover {
  border-color: var(--el-color-primary);
}

.el-icon.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  text-align: center;
}
</style>